package models

import com.jfinal.plugin.activerecord.Model

class ClassificationM: Model<ClassificationM>() {
    companion object{
        const val DEFAULT_CLASS_ID = 13
        const val TABLE_NAME = "classification"
        const val ID = "id"
        const val NAME = "name"
        val AGENT by lazy {
            ClassificationM()
        }
        private val map = HashMap<Int, String>()

        init {
            createClassificationMap()
        }

        fun getAllClassificationName(): List<String> {
            return map.map { it.value }
        }

        fun getIdByName(name: String): Int{
            map.forEach{
                if (it.value == name){
                    return it.key
                }
            }
            return -1
        }

        fun getNameById(id: Int): String{
            return map[id] ?: ""
        }

        private fun createClassificationMap(){
            AGENT.findAll().forEach {
                map[it.getInt(ID)] = it[NAME]
            }
        }
    }
}
